package com.xiaoyu.lanling.qqapi;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;

import com.tencent.connect.share.QQShare;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;
import com.xiaoyu.base.AppConfig;
import com.xiaoyu.lanling.R;
import com.xiaoyu.lanling.activity.base.AppCompatToolbarActivity;
import com.xiaoyu.lanling.common.Constant;
import com.xiaoyu.lanling.event.share.ShareEvent;

public class QQEntryActivity extends AppCompatToolbarActivity implements IUiListener {

    private Tencent mTencent;

    @Override
    protected void onCreateSafelyAfterAppFinishInit(Bundle savedInstanceState) {
        super.onCreateSafelyAfterAppFinishInit(savedInstanceState);
        setContentView(R.layout.qq_entry_activity);

        mTencent = Tencent.createInstance(AppConfig.qqAppId(), this);
        mTencent.shareToQQ(this, createShareBundle(), this);

        findViewById(R.id.layout).setOnClickListener(view -> shareIsSuccess(false));
    }

    @Override
    protected void onResumeSafelyAfterAppFinishInit(boolean isFirstTimeResume) {
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Tencent.onActivityResultData(requestCode, resultCode, data, this);
    }

    @Override
    protected boolean isSetRequestedOrientationPortrait() {
        return Build.VERSION.SDK_INT != Build.VERSION_CODES.O;
    }

    @Override
    public void onComplete(Object o) {
        shareIsSuccess(true);
    }

    @Override
    public void onError(UiError uiError) {
        shareIsSuccess(false);
    }

    @Override
    public void onCancel() {
        shareIsSuccess(false);
    }

    private Bundle createShareBundle() {
        final Bundle bundle = new Bundle();
        final String type = getIntent().getStringExtra(Constant.ShareConfig.SHARE_QQ_TYPE_KEY);
        if (TextUtils.equals(type, Constant.ShareConfig.SHARE_QQ_TYPE_IMAGE_KEY)) {
            bundle.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_IMAGE);
            bundle.putString(QQShare.SHARE_TO_QQ_IMAGE_LOCAL_URL, getIntent().getStringExtra(Constant.ShareConfig.SHARE_QQ_IMAGE_KEY));
        } else {
            bundle.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
            bundle.putString(QQShare.SHARE_TO_QQ_TARGET_URL, getIntent().getStringExtra(Constant.ShareConfig.SHARE_QQ_URL_KEY));
            bundle.putString(QQShare.SHARE_TO_QQ_TITLE, getIntent().getStringExtra(Constant.ShareConfig.SHARE_QQ_TITLE_KEY));
            bundle.putString(QQShare.SHARE_TO_QQ_SUMMARY, getIntent().getStringExtra(Constant.ShareConfig.SHARE_QQ_TEXT_KEY));
            bundle.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, getIntent().getStringExtra(Constant.ShareConfig.SHARE_QQ_IMAGE_KEY));
        }
        if (getIntent().getBooleanExtra(Constant.ShareConfig.SHARE_QQ_IS_QZONE_KEY, false)) {
            bundle.putInt(QQShare.SHARE_TO_QQ_EXT_INT, QQShare.SHARE_TO_QQ_FLAG_QZONE_AUTO_OPEN);
        }
        return bundle;
    }

    private void shareIsSuccess(boolean isSuccess) {
        new ShareEvent(isSuccess).post();
        finish();
    }
}
